package com.hr.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hr.dao.UserDao;
import com.hr.model.UserModel;
import com.hr.service.UserService;
import com.hr.util.PasswordEncoder;
/**
 * 员工service
 * @author fazhen.zheng
 *
 */
@Service
public class UserServiceImpl implements UserService{
	@Autowired
	private UserDao userDao;
	@Override
	public UserModel checkLogin(UserModel userModel) {
		
		if (userModel == null)
			return null;
		List<UserModel> result = userDao.find(userModel.getLogin_name(),
				PasswordEncoder.MD5(userModel.getLogin_password()));
		if(result!=null&&result.size()>0)
			return result.get(0);
		return null;
	}
	
	@Override
	public List<UserModel> findAll() {
		return userDao.findAll();
	}
	
	@Override
	public UserModel findByPrimaryKey(String user_id) {
		if(user_id==null||"".equals(user_id.trim()))
			return null;
		return userDao.queryByPrimaryKey(user_id);
	}
	
	@Override
	public boolean editUser(UserModel userModel) {
		if(userModel==null)
			return false;
		userModel.setValid("1");
		String userId = userModel.getUser_id();
		if(userId!=null&&!"".equals(userId.trim())){
			userDao.update(userModel);
		}else{
			userDao.insert(userModel);
		}
		return true;
	}

	@Override
	public boolean removeUser(String user_id) {
		if(!"".equals(user_id)){
			//TODO 删除员工时，需要对其它的字表进行删除
			return userDao.delete(user_id);
		}
		return false;
	}

	@Override
	public String updatePassword(UserModel user, String oldPassword,
			String newPassword, String newPassword2) {
		if(oldPassword==null||"".equals(oldPassword.trim())){
			return "原始密码不能为空!";
		}
		if(newPassword==null||"".equals(newPassword.trim())){
			return "新密码不能为空!";
		}
		if(!newPassword.equals(newPassword2)){
			return "新密码两次输入不一致!";
		}
		if(!oldPassword.equals(user.getLogin_password())){
			return "原始密码不正确";
		}
		userDao.updatePassword(user.getUser_id(), PasswordEncoder.MD5(newPassword2));
		
		return "密码修改成功";
	}
	
}
